package com.yunlin.utils;

/**
 * Created by lixiaodaoaaa on 2017/8/15.
 */

public class FormatUtils{
    
    
    //举例 01  formatter to  01
    public static String intValueTo2HexString(int value){
        return String.format("%02x", value);
    }
    
    
    //举例 0x01 byte ==>  formatter to  01
    //  0x 2e  byte => str  Hex  "2e"
    public static String byteToHexStr(byte byteValue){
        int intValue=ConvertUtils.byteToInt(byteValue);
        return String.format("%02x", intValue);
    }
    
    
    //举例 01  formatter to  01 00
    public static String intValueTo4HexString(int value){
        return String.format("%04x", value);
    }
    
    //举例 01  formatter to  01 00
    
    /**
     * 时间戳1526289583
     * 变成16进制【5a f9 54 af】
     *
     * @param value
     * @return
     */
    public static String longValueTo8Hex(long value){
        return String.format("%08x", value);
    }
    
    
    /**
     * 将String转化为byte[]数组
     *
     * @param arg 需要转换的String对象
     * @return 转换后的byte[]数组
     * Exam:
     * form :"AABB010A0000007001"
     * To_sHEX_Bytes:AABB010A0000007001
     */
    public static byte[] strCommandToBytes(String arg){
        int len=arg.length();
        byte[] data=new byte[len / 2];
        for (int i=0; i < len; i+=2) {
            data[i / 2]=(byte) ((Character.digit(arg.charAt(i), 16) << 4)
                    + Character.digit(arg.charAt(i + 1), 16));
        }
        return data;
    }
    
    //张帆的
    public static byte[] strToBytes(String key){
        byte[] bytes=new byte[16];
        for (int i=0; i < key.length(); i=i + 2) {
            int val=Integer.parseInt(key.substring(i, i + 2), 16);
            int s=i / 2;
            bytes[s]=(byte) val;
        }
        return bytes;
    }
    
    
    //用张帆的
    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder=new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i=0; i < src.length; i++) {
            int v=src[i] & 0xFF;
            String hv=Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    
    
    /**
     * 重新排序 string
     * 举例输入：00 11
     * 输出   :11 00
     * <p>
     * 输入 11 22
     * 输出 22 11
     *
     * @return
     */
    public static String revertString(String str){
        String data0=str.substring(2, str.length());
        String data1=str.substring(0, 2);
        return data0 + data1;
    }
    
    /**
     * 重新排序 string
     * 输入 00000001
     * 输出：01 00 00 00
     *
     * @return
     */
    public static String revert4String(String str){
        if (str.trim().toString().length() != 8) {
            return str;
        }
        String header=str.substring(0, 4);
        String endBody=str.substring(4, 8);
        return revertString(endBody) + revertString(header);
    }
    
    /**
     * 重新排序 string
     * 输入 01 02 03 04 05 06
     * 输出 06 05 04 03 03 01
     *
     * @return
     */
    public static String revert6String(String str){
        str=str.replace(":", "").replace(" ", "").trim();
        if (str.trim().toString().length() != 12) {
            return str;
        }
        String header=str.substring(0, 8);
        String endBody=str.substring(8, 12);
        return revertString(endBody) + revert4String(header);
    }
    
    /**
     * hexString转 IntValue
     * 举例  Input 10->16
     * 举例 Input 1f -> 31
     *
     * @param hexString
     */
    public static int hexString2IntValue(String hexString){
        if (hexString.trim().length() < 1) {
            return 0;
        }
        return Integer.valueOf(hexString, 16).intValue();
    }
    
    
}
